package kom.gui.interfaces;

import java.util.*;
import kom.Cooldown;
import kom.gfx.Screen;
import kom.gui.interfaces.windows.CornerMenu;
import kom.gui.interfaces.windows.Mapedit;

public class WindowControl {

    HashMap<String, Window> windows = new HashMap<String, Window>();
    // windows that are shown on the screen
    List<String> activeWindows = new ArrayList<String>();
    // window that i just clicked
    public static String clicked = "";
    // focused menu so i would be able to go thro it using keys
    public static String menuAtivo = "";
    Cooldown cdMenu;

    public void loadWindows() {
        // this.carregaJanelasMapedit();
        addWindow(new Mapedit());
        addWindow(new CornerMenu());
        // addJanela(new Acoes());
    }

    public void addWindow(Window qual) {
        this.windows.put(qual.getNome(), qual);
    }

    public Window getWindow(String nome) {
        if (windows.containsKey(nome)) {
            return (Window) windows.get(nome);
        } else {
            return null;
        }
    }

    public void activateWindow(String nome) {
        if (!activeWindows.contains(nome)) {
            activeWindows.add(activeWindows.size(), nome);
            windows.get(nome).setActive(true);
        }
    }

    public void deactivateWindow(String nome) {
        if (activeWindows.contains(nome)) {
            if (clicked.equals(nome)) {
                clicked = "";
            }
            activeWindows.remove(nome);
            windows.get(nome).setActive(false);
        }
    }

    private void renderWindowButtons(Screen screen) {
        for (int x = 0; x < this.activeWindows.size(); x++) {
            windows.get(this.activeWindows.get(x)).renderButtons(screen);
        }
    }

    public void renderWindows(Screen screen) {
        for (int x = 0; x < this.activeWindows.size(); x++) {
            windows.get((String) this.activeWindows.get(x)).render(screen);
        }
        renderWindowButtons(screen);
    }

    public boolean isWindowActive(String nome) {
        if (windows.containsKey(nome)) {
            if (activeWindows.contains(nome)) {
                return true;
            }
        }
        return false;
    }

    public void drag(int x, int y) {
        if (!clicked.equals("")) {
            windows.get(clicked).setMXY(x, y);
            windows.get(clicked).updateWindowPosition();
        }
    }

    public String getClickAction(int x, int y, int button) {
        return runCheckWindows(x, y, button);
    }

    public void releaseWindow() {
        clicked = "";
    }

    private void clickWindow(int sx, int mouseX, int mouseY) {
        clicked = windows.get(activeWindows.get(sx)).getNome();
        windows.get(activeWindows.get(sx)).setMouseX(mouseX);
        windows.get(activeWindows.get(sx)).setMouseY(mouseY);
    }

    private String runCheckWindows(int x, int y, int button) {
        for (int sx = this.activeWindows.size() - 1; sx >= 0; sx--) {
            // se clicou em uma janela
            if (windows.get(activeWindows.get(sx)).clickedMe(x, y)) {
                // passa ela na frente na ordem de desenhars
                String nome = activeWindows.get(sx);
                activeWindows.remove(sx);
                activeWindows.add(nome);
                sx = activeWindows.indexOf(nome);
                // se cliquei em um butaum desta janela
                if (!windows.get(activeWindows.get(sx)).clickedMyButton(x, y).equals("none")) {
                    return windows.get(activeWindows.get(sx)).clickedMyButton(x, y);
                    // se cliquei na janela mas nenhum butaum
                } else {
                    if (button == 1) {
                        clickWindow(sx, x, y);
                        return "none";
                    }
                    if (button == 3) {
                     if(windows.get(activeWindows.get(sx)).closeable)
                        this.deactivateWindow(windows.get(activeWindows.get(sx)).getNome());
                    }
                }
            }
        }
        return "none";
    }

    public List getActiveWindows() {
        return activeWindows;
    }
}
